package com.zhiyou.controller_ym;

import com.google.gson.Gson;
import com.zhiyou.bean.Product;
import com.zhiyou.service.IndexService;
import lombok.Data;
import lombok.ToString;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * @author FightzzZ
 * @date 2022/9/20
 */
@Controller
@RequestMapping("/index")
public class IndexController {

    @Autowired
    IndexService indexService;
    @RequestMapping("/test")
    public void test(HttpServletResponse response) throws IOException {

        List<Product> list = indexService.getSwiperProducts();
        List<Product> list2 = indexService.getGreatProducts();
        List<Product> list3 = indexService.getEduProducts();
        List<Product> list4 = indexService.getPrepareProducts();
        List<Product> list5 = indexService.getCompleteProducts();
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html; charset=UTF-8");
        list.addAll(list2);
        list.addAll(list3);
        list.addAll(list4);
        list.addAll(list5);
        Gson gson = new Gson();
        List<IndexProduct> listIpd = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        System.out.println("++++++++++");
        for (Product product : list) {

            IndexProduct ipd = new IndexProduct();
            ipd.setTitle(product.getTitle());
            ipd.setEndTime(sdf.format(product.getEndTime()));
            ipd.setGetmoney(product.getGetmoney());
            ipd.setGetpeople(product.getGetpeople());
            ipd.setId(product.getId());
            ipd.setImageUrl(product.getImageUrl());
            ipd.setMoney(product.getMoney());
            ipd.setStartTime(sdf.format(product.getStartTime()));
            listIpd.add(ipd);

        }

        System.out.println("============");
        System.out.println(listIpd);

        System.out.println("个数是：" + listIpd.size());
        String pdString = gson.toJson(listIpd);
        System.out.println(pdString);
        response.getWriter().write(pdString);
    }

    @Data
    @ToString
    public class IndexProduct {


        private Integer id;

        private Integer typeId;

        private String title;

        private String imageUrl;

        private Integer money;

        private Integer status;

        private String startTime;

        private String endTime;

        private Double getmoney;

        private Integer getpeople;

    }


}
